package com.librait.mapstracker;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import com.kristijandraca.backgroundmaillibrary.BackgroundMail;
import com.kristijandraca.backgroundmaillibrary.mail.GmailSender;

import android.util.Log;


public class MailService extends GmailSender {
	private String mailhost = "mail.librait.net";
	private Session session;
	
    public MailService(String user, String password) {
		super(user, password);
		// TODO Auto-generated constructor stub
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", mailhost);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "80");
		props.put("mail.smtp.socketFactory.port", "80");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.put("mail.smtp.starttls.enable","true");
		props.setProperty("mail.smtp.quitwait", "false");

		session = Session.getDefaultInstance(props, this);
	}

/* 	public static void sendEmail(String subject) throws AddressException, MessagingException  {
   	String host = "smtp.gmail.com";
    	String from = RegisterActivity.savedData.getString("email", "");
    	String fromPass = RegisterActivity.savedData.getString("password", "");
    	
        String to = "danielzaccagnini@gmail.com";
        
        Multipart multiPart;
        
        String finalString="";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", fromPass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        Log.i("Check", "done pops");
        
        Session session = Session.getInstance(props, null);
        DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" )); 
        MimeMessage message = new MimeMessage(session); 
        message.setFrom(new InternetAddress(from)); 
        message.setDataHandler(handler); 
        message.setSubject(subject);
        Log.i("Check", "done sessions");

        multiPart=new MimeMultipart();
        
        InternetAddress toAddress; 
        toAddress = new InternetAddress(to); 
        message.addRecipient(Message.RecipientType.TO, toAddress); 
        Log.i("Check", "added recipient"); message.setSubject("Send Auto-Mail"); 
        message.setContent(multiPart); 
        message.setText("Demo For Sending Mail in Android Automatically");

        Log.i("check", "transport"); 
        Transport transport = session.getTransport("smtp"); 
        Log.i("check", "connecting"); 
        transport.connect("smtp.gmail.com", from, fromPass);
        Log.i("check", "wana send"); 
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close(); 
        Log.i("check", "sent");
*/
/*    	Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", true); // added this line
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "danielzaccagnini");
        props.put("mail.smtp.password", "Ferrari1.");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", true);



        Session session = Session.getInstance(props,null);
        MimeMessage message = new MimeMessage(session);

        session.setDebug(true);
        System.out.println("Port: "+session.getProperty("mail.smtp.port"));

        // Create the email addresses involved
        try {
            InternetAddress from = new InternetAddress("danielzaccagnini");
            message.setSubject("Yes we can");
            message.setFrom(from);
            message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("danielzaccagnini@gmail.com"));

            // Create a multi-part to combine the parts
            Multipart multipart = new MimeMultipart("alternative");

            // Create your text message part
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("some text to send");

            // Add the text part to the multipart
            multipart.addBodyPart(messageBodyPart);

            // Create the html part
            messageBodyPart = new MimeBodyPart();
            String htmlMessage = "Our html text";
            messageBodyPart.setContent(htmlMessage, "text/html");


            // Add html part to multi part
            multipart.addBodyPart(messageBodyPart);

            // Associate multi-part with message
            message.setContent(multipart);

            // Send message
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", "danielzaccagnini", "Ferrari1.");
            System.out.println("Transport: "+transport.toString());
            transport.sendMessage(message, message.getAllRecipients());


        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }*/
}
